00001 #include "../stdafx.h"
00002 #include "../RightView.h"
00003 #include "TraffEduObject.h"
00004 #include "../0SGWindows/OsgWindows.h"
00005
00006
00007
00008 int ObjectIDs=0;
00009
00010
00011 TraffEduObject::TraffEduObject()
00012 {
00013
00014
00015 objectGroup = new osg::Group();
00016 objectGeode = new osg::Geode();
00017 objectGeometry = new osg::Geometry();
00018 objectPAT=osg::ref_ptr<osg::PositionAttitudeTransform>(
00019 new osg::PositionAttitudeTransform);
00020 objectGeode->addDrawable(objectGeometry.get());
00021 objectPAT->addChild(objectGeode.get());
00022 objectPAT->addChild(objectGroup.get());
00023
00024 m_drotz = 0.0;
00025
00026 viewMode = 1;
00027
00028 condition = 'd';
00029 isBeforeCreated = 0;
00030 physicalVehicle =0;
00031 cameraFollow=0;
00032 }
00033
00034 TraffEduObject::TraffEduObject(string pathname, float scale)
00035 {
00036 cout<<"girdin mi"<<endl;
00037 setModelPath(pathname);
00038 setModelScale(scale);
00039
00040 objectGeode = new osg::Geode();
00041 objectGroup = new osg::Group();
00042 objectGeometry = new osg::Geometry();
00043 objectPAT=osg::ref_ptr<osg::PositionAttitudeTransform>(
00044 new osg::PositionAttitudeTransform);
00045 objectGeode->addDrawable(objectGeometry.get());
00046 objectPAT->addChild(objectGeode.get());
00047 objectPAT->addChild(objectGroup.get());
00048
00049
00050 viewMode = 1;
00051 condition = 'd';
00052 isBeforeCreated = 0;
00053 physicalVehicle =0;
00054 cameraFollow=0;
00055 }
00056
00057 TraffEduObject::~TraffEduObject()
00058 {
00059
00060 }
00061
00062 void TraffEduObject::addTree(osg::ref_ptr<osg::Group> root, int animationFlag)
00063 {
00064
00065 int index;
00066
00067 int length=objectPATvector.size();
00068 if(type==VEHICLE ){
00069 if(animationFlag==-1){
00070 index=osgwind->returnIndex(osgwind->getCurrentFrameId());
00071 if(index!=-1){
00072 for(int j=0;j<length;++j){
00073 root->removeChild(objectPATvector[j]->objectPAT.get());
00074 }
00075
00076 root->addChild(objectPATvector[index]->objectPAT.get());
00077 }
00078 }
00079 else{
00080 root->addChild(objectPAT.get());
00081 }
00082 }
00083
00084 else{
00085 root->addChild(objectPAT.get());
00086 }
00087 }
00088
00089 void TraffEduObject::setRotate(float angle,float x, float y, float z)
00090 {
00091 m_drotz += angle;
00092
00093
00094 int index;
00095 if(type==VEHICLE && getInAnimation()!=1){
00096 index=osgwind->returnIndex(osgwind->getCurrentFrameId());
00097 if(index!=-1){
00098 objectPATvector[index]->objectPAT->setAttitude(osg::Quat(osg::inDegrees(m_drotz),osg::Vec3(x,y,z)));
00099 }
00100 }
00101 else{
00102 objectPAT->setAttitude(osg::Quat(osg::inDegrees(m_drotz),osg::Vec3(x,y,z)));
00103 }
00104 }
00105
00106
00107 void TraffEduObject::setPosition(float x, float y, float z)
00108 {
00109
00110 int index;
00111 if(type==VEHICLE && getInAnimation()!=1){
00112 index=osgwind->returnIndex(osgwind->getCurrentFrameId());
00113 if(index!=-1){
00114 objectPATvector[index]->objectPAT->setPosition(osg::Vec3d(x,y,z));
00115 }
00116 }
00117 else{
00118 objectPAT->setPosition(osg::Vec3d(x,y,z));
00119 }
00120 }
00121
00122 void TraffEduObject::setScale(float x, float y, float z)
00123 {
00124
00125 int index;
00126 if(type==VEHICLE && getInAnimation()!=1){
00127 index=osgwind->returnIndex(osgwind->getCurrentFrameId());
00128 if(index!=-1&& objectPATvector[index]!=NULL){
00129 objectPATvector[index]->objectPAT->setScale(osg::Vec3d(x,y,z));
00130 }
00131 }
00132 else{
00133 objectPAT->setScale(osg::Vec3d(x,y,z));
00134 }
00135 }
00136
00137 osg::Vec3d TraffEduObject::getScale()
00138 {
00139 int index;
00140 if(type==VEHICLE && getInAnimation()!=1){
00141 index=osgwind->returnIndex(osgwind->getCurrentFrameId());
00142 if(index!=-1){
00143 return objectPATvector[index]->objectPAT->getScale();
00144 }
00145 }
00146 else{
00147 return objectPAT->getScale();
00148 }
00149 }
00150
00151 osg::Vec3d TraffEduObject::getPosition()
00152 {
00153 int index;
00154 if(type==VEHICLE && getInAnimation()!=1){
00155 index=osgwind->returnIndex(osgwind->getCurrentFrameId());
00156 if(index!=-1){
00157 return objectPATvector[index]->objectPAT->getPosition();
00158 }
00159 }
00160 else{
00161 return objectPAT->getPosition();
00162 }
00163 }
00164
00165 osg::Quat TraffEduObject::getRotate()
00166 {
00167 int index;
00168 if(type==VEHICLE && getInAnimation()!=1){
00169 index=osgwind->returnIndex(osgwind->getCurrentFrameId());
00170 if(index!=-1){
00171 return objectPATvector[index]->objectPAT->getAttitude();
00172 }
00173 }
00174 else{
00175 return objectPAT->getAttitude();
00176 }
00177 }
00178
00179
00180 osg::PositionAttitudeTransform* TraffEduObject::getPAT()
00181 {
00182 int index;
00183 if(type==VEHICLE && getInAnimation()!=1){
00184 index=osgwind->returnIndex(osgwind->getCurrentFrameId());
00185 if(index!=-1){
00186 return objectPATvector[index]->objectPAT.get();
00187 }
00188 }
00189 else{
00190 return objectPAT.get();
00191 }
00192 }
00193
00194 void TraffEduObject::hide(osg::ref_ptr<osg::Group> root)
00195 {
00196 if(viewMode==1)
00197 {
00198 int index;
00199 if(type==VEHICLE && getInAnimation()!=1){
00200 index=objectPATvector.size();
00201 if(index!=-1){
00202 for(int j=0;j<index;++j){
00203 root->removeChild(objectPATvector[j]->objectPAT.get());
00204 }
00205
00206 }
00207 }
00208 else{
00209 root->removeChild(objectPAT.get());
00210 }
00211 viewMode=0;
00212
00213 }
00214 }
00215
00216 void TraffEduObject::display(osg::ref_ptr<osg::Group> root,int animationFlag)
00217 {
00218 if(viewMode==0)
00219 {
00220 int index;
00221 int length=objectPATvector.size();
00222 if(type==VEHICLE && getInAnimation()!=1){
00223 if(animationFlag==-1){
00224 index=osgwind->returnIndex(osgwind->getCurrentFrameId());
00225 if(index!=-1){
00226 for(int j=0;j<length;++j){
00227 root->removeChild(objectPATvector[j]->objectPAT.get());
00228 }
00229 root->addChild(objectPATvector[index]->objectPAT.get());
00230 }
00231 }
00232 else{
00233 root->addChild(objectPAT.get());
00234 }
00235 }
00236
00237 else{
00238 root->addChild(objectPAT.get());
00239 }
00240 viewMode=1;
00241 }
00242
00243 }
00244
00245 void TraffEduObject::setModelPath(string name)
00246 {
00247 modelPath = name;
00248 }
00249
00250 string TraffEduObject::getModelPath()
00251 {
00252 return modelPath;
00253 }
00254 osg::ref_ptr<osg::Node> TraffEduObject::returnNode(){
00255 node = osgDB::readNodeFile( modelPath );
00256 return node;
00257 }
00258
00259 osg::ref_ptr<osg::Node> TraffEduObject::returnNode(int modelNo){
00260 node=CarModels::returnModel(modelNo);
00261 return node;
00262 }
00263
00264 void TraffEduObject::setObjectType(ObjectType objectType){
00265 type= objectType;
00266 }
00267
00268 ObjectType TraffEduObject::getObjectType(){
00269 return type;
00270 }
00271
00272 void TraffEduObject::setModelScale(float scale){
00273 modelScale=scale;
00274 }
00275
00276 float TraffEduObject::getModelScale(){
00277 return modelScale;
00278 }
00279
00280 void TraffEduObject::setCondition(char c)
00281 {
00282
00283 condition = c;
00284 }
00285
00286 char TraffEduObject::getCondition()
00287 {
00288 return condition;
00289 }
00290
00291 int TraffEduObject::getObjectId()
00292 {
00293 return objectId;
00294 }
00295
00296 void TraffEduObject::setObjectId()
00297 {
00298
00299 objectId = ObjectIDs++;
00300
00301 }
00302
00303 osg::ref_ptr<osg::PositionAttitudeTransform> TraffEduObject::getObjectPAT(){
00304 return objectPAT;
00305 }
00306
00307 int TraffEduObject::getIsBeforeCreated()
00308 {
00309 return isBeforeCreated;
00310 }
00311 void TraffEduObject::resetModelPath(string flagVehicleWheel){
00312
00313 int i=0, where=0;
00314 int j=0, stringLength=0;
00315 where= modelPath.find_last_of ('/');
00316 string temp=modelPath.substr(0,where+1);
00317
00318 if(flagVehicleWheel=="Vehicle"){
00319 temp=temp+"kasa0da.osg";
00320 }
00321 else if(flagVehicleWheel=="sol_teker"){
00322 temp=temp+"sol_teker.osg";
00323 }
00324 else if(flagVehicleWheel=="sag_teker"){
00325 temp=temp+"sag_teker.osg";
00326 }
00327 else{
00328 temp=temp+"butun.osg";
00329 }
00330
00331
00332 modelPath.clear();
00333 setModelPath(temp);
00334 }
00335
00336 int TraffEduObject::addObjectPAT(int frameId){
00337
00338 int index=0;
00339 int index2=0;
00340 if(type==VEHICLE){
00341 index =osgwind->returnIndex(frameId);
00342 if(index==0){
00343 if(index+1 == osgwind->sizeOfFrames()){
00344 return -1;
00345 }
00346 else{
00347 index+=1;
00348 }
00349 }
00350 else {
00351 index-=1;
00352 }
00353 if(index>=0){
00354 index2=returnIndex(osgwind->getFrames()[index]->getFrameId());
00355 if(index2>=0){
00356 vehiclePAT *temp=new vehiclePAT [1] ;
00357 temp->frameID=frameId;
00358 temp->objectPAT=osg::ref_ptr<osg::PositionAttitudeTransform>(
00359 new osg::PositionAttitudeTransform(*(objectPATvector[index2]->objectPAT),osg::CopyOp::SHALLOW_COPY));
00360 temp->objectPAT->addChild(objectGeode.get());
00361 temp->objectPAT->addChild(objectGroup.get());
00362 objectPATvector.push_back(temp);
00363 sortObjectPATvector();
00364 return 1;
00365 }
00366 else{
00367 return -1;
00368 }
00369 }
00370 else return -1;
00371 }
00372 else return -1;
00373 }
00374
00375 int TraffEduObject::deleteObjectPAT(int frameId){
00376 if(type==VEHICLE){
00377 int length=objectPATvector.size();
00378 for(int i=0;i<length;++i){
00379 if(objectPATvector[i]->frameID==frameId){
00380 osgwind->getConstantRoot()->removeChild(objectPATvector[i]->objectPAT.get());
00381 objectPATvector.erase(objectPATvector.begin()+i);
00382 return 1;
00383 }
00384 }
00385 return -1;
00386 }
00387 else return -1;
00388 }
00389
00390 void TraffEduObject::sortObjectPATvector(){
00391 int j,i,key;
00392 int length=objectPATvector.size();
00393 vehiclePAT * temp;
00394
00395 for( j=1;j<length;++j){
00396 temp=objectPATvector[j];
00397 key=objectPATvector[j]->frameID;
00398 i=j-1;
00399 while(i>=0 && objectPATvector[i]->frameID >key){
00400 objectPATvector[i+1]=objectPATvector[i];
00401 i-=1;
00402 }
00403 objectPATvector[i+1]=temp;
00404 }
00405 }
00406
00407 osg::ref_ptr<osg::PositionAttitudeTransform> TraffEduObject::reloadObjectPAT(){
00408 sortObjectPATvector();
00409 if(objectPATvector.size()!=0){
00410
00411 objectPAT=osg::ref_ptr<osg::PositionAttitudeTransform>(
00412 new osg::PositionAttitudeTransform(*objectPATvector[0]->objectPAT,osg::CopyOp::SHALLOW_COPY));
00413
00414 }
00415 return objectPAT.get();
00416
00417 }
00418
00419
00420 void TraffEduObject::setInAnimation(int flag){
00421 inAnimation=flag;
00422
00423 }
00424 int TraffEduObject::getInAnimation(){
00425 return inAnimation;
00426
00427 }
00428 int TraffEduObject::objectPATvectorSize(){
00429 return objectPATvector.size();
00430 }
00431 vector<vehiclePAT *>& TraffEduObject::getObjectPATvector(){
00432 return objectPATvector;
00433 }
00434 void TraffEduObject::setAsPhysicalVehicle(){
00435 if(type==VEHICLE)
00436 physicalVehicle=1;
00437 }
00438 bool TraffEduObject::isPhysicalVehicle(){
00439 return physicalVehicle==1;
00440 }
00441
00442 void TraffEduObject::resetObjectId()
00443 {
00444 ObjectIDs=0;
00445 }
00446 int TraffEduObject::returnIndex(int frameID){
00447 int length=objectPATvector.size();
00448 for(int i=0; i<length;++i){
00449 if(frameID==(objectPATvector[i]->frameID))
00450 return i;
00451 }
00452 return -1;
00453
00454 }
00455 void TraffEduObject::setCameraFollow(){
00456 cameraFollow=1;
00457 }
00458 bool TraffEduObject::isCameraFollow(){
00459 return cameraFollow==1;
00460 }
00461 void TraffEduObject::resetCameraFollow(){
00462 cameraFollow=0;
00463 }
00464 void TraffEduObject::undoPhysicalVehicle(){
00465 physicalVehicle=0;
00466 }